package com.geuso.xingdutch.numbers;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.geuso.xingdutch.R;
import com.geuso.xingdutch.util.SoundUtil;


public class NumbersListAdapter extends ArrayAdapter<DutchNumber>{

	private static final int resource = R.layout.dutch_number_row;
	
	public NumbersListAdapter(Context context,	ArrayList<DutchNumber> numbers) {
		super(context, resource, numbers);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		// assign the view we are converting to a local variable
		View v = convertView;

		// first check to see if the view is null. if so, we have to inflate it.
		// to inflate it basically means to render, or show, the view.
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(resource, null);
		}

		DutchNumber n = getItem(position);
		if (n != null) {
			
			TextView dn_id = (TextView) v.findViewById(R.id.dutch_number_id);
			TextView dutch = (TextView) v.findViewById(R.id.dutch_number_word);
			TextView dutch_pronounciation = (TextView) v.findViewById(R.id.dutch_number_pronounciation);
			ImageView play_sound = (ImageView) v.findViewById(R.id.dutch_number_play_voice);
			
			if (dn_id != null){
				dn_id.setText(n.getNumber_id() + "");
			}
			if (dutch != null){
				dutch.setText(n.getDutch());
			}
			if (dutch_pronounciation != null){
				dutch_pronounciation.setText(n.getDutch_pronounciation());
			}

			
			if (play_sound != null){
				if (n.getSound_file() > 0){
					play_sound.setVisibility(View.VISIBLE);
					SoundUtil.setOnClickPlaySound(play_sound, n.getSound_file());
				}
				else play_sound.setVisibility(View.INVISIBLE);
				
			}
			
			
		}
		
		return v;
	}
	
	
	
}
